package com.example.demo.services;

import com.example.demo.domain.Category;
import com.example.demo.domain.CategoryExample;
import com.example.demo.domain.Ebook;
import com.example.demo.domain.EbookExample;
import com.example.demo.mapper.CategoryMapper;
import com.example.demo.mapper.EbookMapper;
import com.example.demo.req.EbookQueryReq;
import com.example.demo.req.EbookSaveReq;
import com.example.demo.resp.PageResp;
import com.example.demo.util.CopyUtil;
import com.example.demo.util.FormatterCategory;
import com.example.demo.util.SnowFlake;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.List;

@Service
public class EbookService {
    private static final Logger LOG = LoggerFactory.getLogger(EbookService.class);

    @Resource
    private EbookMapper ebookMapper;

    @Resource
    private SnowFlake snowFlake;

    @Resource
    private CategoryMapper categoryMapper;

    public PageResp<Ebook> list(EbookQueryReq req) {

        EbookExample ebookExample = new EbookExample();
        EbookExample.Criteria criteria = ebookExample.createCriteria();
        if(!ObjectUtils.isEmpty(req.getName())) {
            criteria.andNameLike("%" + req.getName() + "%");
        }

        if(!ObjectUtils.isEmpty(req.getCategory1Id())) {
            criteria.andCategory1IdEqualTo(req.getCategory1Id());
        }

        if(!ObjectUtils.isEmpty(req.getCategory2Id())) {
            criteria.andCategory2IdEqualTo(req.getCategory2Id());
        }

        PageHelper.startPage(req.getPage(),req.getSize());
        List<Ebook> ebookList = ebookMapper.selectByExample(ebookExample);

        CategoryExample categoryExample = new CategoryExample();
        List<Category> categoryList = categoryMapper.selectByExample(categoryExample);
//        LOG.info("分类数据"+categoryList);
        FormatterCategory formatterCategory = new FormatterCategory(categoryList);
        for (Ebook ebook : ebookList) {
            ebook.setCategory1Name(formatterCategory.formattrCategoryName(ebook.getCategory1Id()));
            ebook.setCategory2Name(formatterCategory.formattrCategoryName(ebook.getCategory2Id()));
        }

        PageInfo<Ebook> pageInfo = new PageInfo<>(ebookList);

//        LOG.info("总页数:" + pageInfo.getPages());
//        LOG.info("总行数:" + pageInfo.getTotal());


//        List<EbookResp> respList = new ArrayList<>();
//        for (Ebook ebook : ebookList) {
//            EbookResp ebookResp = CopyUtil.copy(ebook, EbookResp.class);
//            respList.add(ebookResp);
//        }

//        return CopyUtil.copyList(ebookList, EbookResp.class);
        PageResp<Ebook> pageResp = new PageResp<>();
        pageResp.setTotal(pageInfo.getTotal());
        pageResp.setList(ebookList);
        return pageResp;
    }

    public void save(EbookSaveReq req){
        EbookExample ebookExample = new EbookExample();
        EbookExample.Criteria criteria = ebookExample.createCriteria();
        criteria.andIdEqualTo(req.getId());
        Ebook ebook = CopyUtil.copy(req, Ebook.class);
        if(ObjectUtils.isEmpty(req.getId())){
            ebook.setId(snowFlake.nextId());
            ebookMapper.insert(ebook);
        }else{
            ebookMapper.updateByExampleWithBLOBs(ebook,ebookExample);
        }

    }

    public Ebook getDetail(EbookQueryReq req){
//        EbookExample ebookExample = new EbookExample();
//        EbookExample.Criteria criteria = ebookExample.createCriteria();
//        criteria.andIdEqualTo(req.getId());
        Ebook ebook = ebookMapper.selectByPrimaryKey(req.getId());
        return ebook;
    }

    public void delete(EbookSaveReq req){
        ebookMapper.deleteByPrimaryKey(req.getId());
    }

}
